package com.exer.basic.map;


import java.util.*;

/**
 * 3.3.4 TreeMap
 * TreeMap完全由红黑树实现
 */
public class TreeMapSample {
    public static void main(String[] args) {
        Map<Long, String> map = new HashMap<>();
        map.put(1l, "James");
        map.put(20l, "Paul");
        map.put(5l, "Rose");
        System.out.println("HashMap 无序");
        for (Iterator<Long> it = map.keySet().iterator(); it.hasNext(); ) {
            long key = it.next();
            String value = map.get(key);
            System.out.println(key + " " + value);
        }
        //TreeMap
        System.out.println("TreeMap 升序");
        TreeMap<Long, String> treeMap = new TreeMap<>();
        treeMap.putAll(map);
        for (Iterator<Long> it = treeMap.keySet().iterator(); it.hasNext(); ) {
            long key = it.next();
            String value = map.get(key);
            System.out.println(key + " " + value);
        }
        System.out.println("TreeMap 降序");
        TreeMap<Long, String> treeMap2 = new TreeMap<>(Collections.reverseOrder());
        treeMap2.putAll(map);
        for (Iterator<Long> it = treeMap2.keySet().iterator(); it.hasNext(); ) {
            long key = it.next();
            String value = map.get(key);
            System.out.println(key + " " + value);
        }

    }
}
